package wqh.intercept;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import wqh.global.GlobalSession;
import wqh.service.AlarmInCommingService;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ViewInterception implements HandlerInterceptor{

    @Autowired
    private AlarmInCommingService alarmInCommingService;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        if(!GlobalSession.existSessionID(request.getSession().getId()))
        {
            modelAndView.setViewName("ftl_login");
            modelAndView.addObject("error","登录超时，请重新登录。");
        }
        else
        {
            int alarm = alarmInCommingService.countAllStatus("火警");
            int fault = alarmInCommingService.countAllStatus("故障");
            int falseAlarm = alarmInCommingService.countAllStatus("误报");
            modelAndView.addObject("fireLog",alarm);
            modelAndView.addObject("faultLog",fault);
            modelAndView.addObject("falseLog",falseAlarm);
            modelAndView.addObject("totalLog",alarm + falseAlarm + fault);
//            这里可以弄权限
        }
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
